package com.saydc.forms;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import com.saydc.beans.Usuario;
import com.saydc.dao.UsuariosDAO;

public class LoginForm extends ActionForm {
	
	private static final long serialVersionUID = 1L;
	private String usuario;
	private String password;
	
	public String getUsuario() {
		return usuario;
	}
	public void setUsuario(String usuario) {
		this.usuario = usuario;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		if (this.getUsuario().length() == 0) {
			errors.add("userRequired", new ActionMessage("error.usuario.required"));
		}
		if (this.getPassword().length() == 0) {
			errors.add("passRequired", new ActionMessage("error.contrasenia.required"));
		}
		UsuariosDAO dao = UsuariosDAO.getInstance();
		Usuario usuarioLogueado = dao.getUsuario(usuario);
		if(usuarioLogueado == null || usuarioLogueado.getContrasenia().equals(password) == false){
			errors.add("loginIncorrecto", new ActionMessage("error.login.incorrecto"));
		}
		return errors;
	}

}
